package com.tisumoon.share;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.google.gson.Gson;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQAuth;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tisumoon.Const;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.wei.c.utils.SPref;

public class QQ {
	public static String UM_STR				= "com.umeng.share";

	private QzoneShare mQzoneShare;
	private QQShare mQShare;
	private QQAuth mQAuth;
	private UMSocialService mController;
	private Context mAppContext;

	public QQ(Context context, String appId) {
		mAppContext = context.getApplicationContext();
		mQAuth = QQAuth.createInstance(appId, mAppContext);
	}

	public void login(Activity activity, String apiScope, IUiListener l) {
		String json = getLoginResultJson(activity);
		LoginResult result = null;
		try { result = new Gson().fromJson(json, LoginResult.class); } catch (Exception e) {}
		new Gson().fromJson(json, LoginResult.class);
		if (result != null && mQAuth.isSessionValid()) {
			l.onComplete(json);
		} else {
			//tencent.loginWithOEM(activity, apiScope, l,"10000144", "10000144", "xxxx");
			mQAuth.login(activity, apiScope, l);
		}
	}

	public void logout(Context context) {
		mQAuth.logout(context);
	}

	public void getUserinfo(IUiListener listener) {	//跟登录不一样，无论本地是否有保持，都从网络更新
		new UserInfo(mAppContext, mQAuth.getQQToken()).getUserInfo(listener);
	}

	private QQShare getQShare() {
		if (mQShare == null) mQShare = new QQShare(mAppContext, mQAuth.getQQToken());
		return mQShare;
	}

	private QzoneShare getQzoneShare() {
		if (mQzoneShare == null) mQzoneShare = new QzoneShare(mAppContext, mQAuth.getQQToken());
		return mQzoneShare;
	}

	private UMSocialService getUMSocialService() {
		if (mController == null) mController = UMServiceFactory.getUMSocialService(UM_STR);
		return mController;
	}

	public void shareToQQImageText(final Activity activity, String title, String content, String clickToUserUrl, String shareImgUrl, final IUiListener l) {
		final Bundle params = new Bundle();
		params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);	//必填，图文分享(普通分享)填Tencent.SHARE_TO_QQ_TYPE_DEFAULT
		params.putString(QQShare.SHARE_TO_QQ_TITLE, title);	//必选，分享的标题, 最长30个字符
		params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content);	//可选，分享的消息摘要，最长40个字符
		params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, clickToUserUrl);	//必填，这条分享消息被好友点击后的跳转URL
		params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareImgUrl);	//可选，分享图片的URL或者本地路径
		params.putString(QQShare.SHARE_TO_QQ_APP_NAME, activity.getString(activity.getApplicationInfo().labelRes));	//可选，手Q客户端顶部，替换“返回”按钮文字，如果为空，用返回代替
		/*params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,  );	/*可选，分享额外选项，两种类型可选（默认是不隐藏分享到QZone按钮且不自动打开分享到QZone的对话框）：
	    QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN，分享时自动打开分享到QZone的对话框。
	    QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE，分享时隐藏分享到QZone按钮*/
		getQShare().shareToQQ(activity, params, l);
	}

	public void shareToQzone(final Activity activity, String title, String content, String clickToUserUrl, String shareImgUrl, final IUiListener l) {
		final Bundle params = new Bundle();
		ArrayList<String> imageUrls = new ArrayList<String>();
		imageUrls.add(shareImgUrl);

		params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);	//必填
		params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);// 必填
		params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, content);// 选填
		params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, clickToUserUrl);// 必填
		params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);

		getQzoneShare().shareToQzone(activity, params, l);
	}

	public void shareToWXFriend(final Context context, String wxAppId, String title, String content, String clickToUserUrl, int shareImgDrawable, final SnsPostListener l) {
		UMWXHandler wxHandler = new UMWXHandler(context, wxAppId);
		wxHandler.addToSocialSDK();
		WeiXinShareContent wxMedia = new WeiXinShareContent();
		wxMedia.setTitle(title);
		wxMedia.setShareContent(content);
		wxMedia.setShareImage(new UMImage(context, shareImgDrawable));
		wxMedia.setTargetUrl(clickToUserUrl);
		getUMSocialService().setShareMedia(wxMedia);
		getUMSocialService().directShare(context, SHARE_MEDIA.WEIXIN, l);
	}

	public void shareToWXCircle(final Context context, String wxAppId, String title, String content, String clickToUserUrl, int shareImgDrawable, final SnsPostListener l) {
		UMWXHandler wxCircleHandler = new UMWXHandler(context, wxAppId);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();
		CircleShareContent circleMedia = new CircleShareContent();
		circleMedia.setTitle(title);
		circleMedia.setShareContent(content);
		circleMedia.setShareImage(new UMImage(context, shareImgDrawable));
		circleMedia.setTargetUrl(clickToUserUrl);
		getUMSocialService().setShareMedia(circleMedia);
		getUMSocialService().directShare(context, SHARE_MEDIA.WEIXIN_CIRCLE, l);
	}

	public static void saveLoginResult(Context context, String json) {
		SPref.saveAsFile(context, Const.ThirdAccount.sSharedPrefName + "." + Const.ThirdAccount.sKey_qq_login_json, json);
	}

	public static LoginResult getLoginResult(Context context) {
		String json = getLoginResultJson(context);
		return json == null ? null : new Gson().fromJson(json, LoginResult.class);
	}

	public static String getLoginResultJson(Context context) {
		return SPref.getFromFile(context, Const.ThirdAccount.sSharedPrefName + "." + Const.ThirdAccount.sKey_qq_login_json);
	}

	public static void saveUserInfoResult(Context context, String json) {
		SPref.saveAsFile(context, Const.ThirdAccount.sSharedPrefName + "." + Const.ThirdAccount.sKey_qq_user_info_json, json);
	}

	public static UserInfoResult getUserInfoResult(Context context) {
		String json = getUserInfoResultJson(context);
		return json == null ? null : new Gson().fromJson(json, UserInfoResult.class);
	}

	public static String getUserInfoResultJson(Context context) {
		return SPref.getFromFile(context, Const.ThirdAccount.sSharedPrefName + "." + Const.ThirdAccount.sKey_qq_user_info_json);
	}

	/**
	 <pre>
	 {
	"ret":0,
	"pay_token":"3ADB702B6C96C325C6DA4BD2237D31C9",
	"pf":"desktop_m_qq-10000144-android-10000144-xxxx",
	"query_authority_cost":232,
	"authority_cost":6910,
	"openid":"B8549E53E408232C6911AC4F20980269",
	"expires_in":7776000,
	"pfkey":"6f3597335afbc02e13dd71730010e03a",
	"msg":"",
	"access_token":"68F003CA6096B658CBB5B81A3103D6BD",
	"login_cost":1412
	}
	</pre>
	 */
	public static class LoginResult {
		public int ret;
		public int expires_in;
		public int login_cost;
		public int authority_cost;
		public int query_authority_cost;
		public String pf;
		public String pfkey;
		public String openid;
		public String pay_token;
		public String access_token;
		public String msg;
	}

	/**
	 <pre>
	 {
		"is_yellow_year_vip":"0",
		"ret":0,
		"figureurl_qq_1":"http:\/\/q.qlogo.cn\/qqapp\/1101766923\/B8549E53E408232C6911AC4F20980269\/40",
		"figureurl_qq_2":"http:\/\/q.qlogo.cn\/qqapp\/1101766923\/B8549E53E408232C6911AC4F20980269\/100",
		"nickname":" ToDo",
		"yellow_vip_level":"0",
		"is_lost":0,
		"msg":"",
		"city":"海淀",
		"vip":"0",
		"level":"0",
		"province":"北京",
		"is_yellow_vip":"0",
		"gender":"男",
		"figureurl":  "http:\/\/qzapp.qlogo.cn\/qzapp\/1101766923\/B8549E53E408232C6911AC4F20980269\/30"
		"figureurl_1":"http:\/\/qzapp.qlogo.cn\/qzapp\/1101766923\/B8549E53E408232C6911AC4F20980269\/50",
		"figureurl_2":"http:\/\/qzapp.qlogo.cn\/qzapp\/1101766923\/B8549E53E408232C6911AC4F20980269\/100",
	}
	</pre>
	 */
	public static class UserInfoResult {
		public int ret;
		/**QQ空间头像**/
		public String figureurl_qq_1;
		public String figureurl_qq_2;
		public String nickname;
		public String is_yellow_vip;
		public String is_yellow_year_vip;
		public String yellow_vip_level;
		public int is_lost;
		public String msg;
		public String city;
		public String vip;
		public String level;
		public String province;
		public String gender;
		/**QQ空间头像**/
		public String figureurl;
		public String figureurl_1;
		public String figureurl_2;

	}
}
